var CalculateDataViewUtil = function() {

    return {
        getLogFrequency : function(){
            var logFrequency = '' || [];
            logFrequency.push(1); 
            logFrequency.push(2); 
            logFrequency.push(3); 
            logFrequency.push(4); 
            logFrequency.push(5); 
            logFrequency.push(10);
            logFrequency.push(15);
            logFrequency.push(30); 
            logFrequency.push(45); 
            logFrequency.push(60); 
            logFrequency.push(120); 
            logFrequency.push(180); 
            logFrequency.push(240); 
            logFrequency.push(300); 
            logFrequency.push(600); 
            logFrequency.push(900); 
            logFrequency.push(1800); 
            return logFrequency;
        },
        sortArray : function(arr){
            for(var i = 0 ; i < arr.length - 1; i++ ){
                for(var j = i+1 ; j < arr.length ; j++){
                    if(arr[i].logFrequency < arr[j].logFrequency ){
                        var temp = arr[i].logFrequency;
                        arr[i].logFrequency = arr[j].logFrequency;
                        arr[j].logFrequency = temp;
                    }
                }
            }
            return arr;
        },
        avgAdd : function (param ,dateToGetRecord,avgAdd){
            var recordParamToDay = ((((8+4)*3600)/param.logFrequency)*24)/12;
            var recordForDateToGet = recordParamToDay * dateToGetRecord;
            return recordForDateToGet / avgAdd;
        },
        printParam : function(param){
            console.log("id :" + param.id);
            console.log("logFrequency :" + param.logFrequency);
            console.log("avgAdd :" + param.avgAdd);
            console.log("numOfRecord  :" + param.numOfRecord);
            console.log("====================");
        },
    };
}();

var ViewCalculateData = Backbone.View.extend({
    initialize : function(options) {
        console.log("View initialize");
        this.name = "";
        this.tpl = options.tpl;
    },

    render : function() {
        var renderHtml = _.template(this.tpl, {
        });
        return this.$el.html(renderHtml);
    },
    close : function() {
        delete this.tpl;
        this.unbind();
        this.remove();
        delete this.$el;
        delete this.el;
    },
    events : {
        "click button[id='btn-click']" : "showCalculate",
    },
    showCalculate : function(){
        var params = this.createParam();
        this.calculateRecord(params,50000,50);
    },
    createParam : function (){
        var params = '' || [];
        var logFrequency = CalculateDataViewUtil.getLogFrequency();
        for(var i = 0 ; i < 19 ; i++){
            var param = '' || {};
            param['id'] = 'Param ' + i;
            param['logFrequency'] = logFrequency[Math.floor((Math.random() * logFrequency.length))];
            param['avgAdd'] = '';
            param['numOfRecord'] = 0;
            params.push(param);
        }
        return params;
    },
    createParam01 : function (){
        var params = '' || [];
        var param = {};
        param['id'] = 'Param 0' ;
        param['logFrequency'] = 300;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 1' ;
        param['logFrequency'] = 300;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 2' ;
        param['logFrequency'] = 60;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 3' ;
        param['logFrequency'] = 60;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 4' ;
        param['logFrequency'] = 60;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 5' ;
        param['logFrequency'] = 30;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 6' ;
        param['logFrequency'] = 30;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 7' ;
        param['logFrequency'] = 30;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 8' ;
        param['logFrequency'] = 1;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        param = '' || {};
        param['id'] = 'Param 9' ;
        param['logFrequency'] = 1;
        param['avgAdd'] = '';
        param['numOfRecord'] = 0;
        params.push(param);
        
        return params;
    },
    calculateRecord : function (params,maxRecordToget,dateToRecord){
        var maxRecord = maxRecordToget;
        var totalRecordFinal = 0;
        var paramsCalculateFinal = '' || [];
        var totalRecordOfParam = 0;
        for(var i = 0 ; i < params.length ; i++){
            var recordParamToDay = ((((8+4)*3600)/params[i].logFrequency)*24)/12;
            var recordForDateToGet = recordParamToDay * dateToRecord;
            totalRecordOfParam += recordForDateToGet;
        }
        if(totalRecordOfParam < maxRecordToget){
            return ;
        } else {
            var avgLineOfParam = maxRecordToget / params.length;
            var paramCalculate = '' || [];
            var recordToUse = 0;
            for(var i = 0 ; i < params.length ; i++){
                var recordParamToDay = ((((8+4)*3600)/params[i].logFrequency)*24)/12;
                var recordForDateToGet = recordParamToDay * dateToRecord;
                if(recordForDateToGet > avgLineOfParam){
                    paramCalculate.push(params[i]);
                } else {
//                    totalRecordFinal += recordForDateToGet;
                    recordToUse += recordForDateToGet;
                    params[i].avgAdd = 1;
                    params[i].numOfRecord = recordForDateToGet;
//                    CalculateDataViewUtil.printParam(params[i]);
                    //===========
                    paramsCalculateFinal.push(params[i]);
                    //===========
                }
            }
            maxRecordToget = maxRecordToget - recordToUse;
            avgLineOfParam = maxRecordToget / paramCalculate.length;
            
            paramCalculate = CalculateDataViewUtil.sortArray(paramCalculate);
            
            var numOfParamCalculate = paramCalculate.length;
            for(var i = 0 ;i < paramCalculate.length ; i++){
                var avgAdd = 2;
                var recordAvgAdd = CalculateDataViewUtil.avgAdd(paramCalculate[i],dateToRecord,avgAdd);
                while(recordAvgAdd > avgLineOfParam){
                    avgAdd++;
                    recordAvgAdd = CalculateDataViewUtil.avgAdd(paramCalculate[i],dateToRecord,avgAdd);
                }
//                totalRecordFinal += recordAvgAdd;
                
                paramCalculate[i].avgAdd = avgAdd;
                paramCalculate[i].numOfRecord = recordAvgAdd;
//                CalculateDataViewUtil.printParam(paramCalculate[i]);
                //===========================================
                paramsCalculateFinal.push(paramCalculate[i]);
                //===========================================
//                paramCalculate.splice(i,1);
                numOfParamCalculate--;
                maxRecordToget = maxRecordToget - recordAvgAdd;
//                avgLineOfParam = maxRecordToget / paramCalculate.length;
                avgLineOfParam = maxRecordToget / numOfParamCalculate;
            }
        }
        for(var i = 0 ;i < paramsCalculateFinal.length ;i++){
            CalculateDataViewUtil.printParam(paramsCalculateFinal[i]);
            totalRecordFinal += paramsCalculateFinal[i].logFrequency;
        }
        console.log("maxRecordToget :" + maxRecord);
        console.log("dateToRecord :" + dateToRecord);
        console.log("totalRecordFinal :" + totalRecordFinal);
    },
});